package com.hwl.lab.controller;

import com.hwl.lab.entity.*;
import com.hwl.lab.repository.BaoxiaoRepository;
import com.hwl.lab.repository.PeplesRepository;
import com.hwl.lab.repository.ZhengRepository;
import com.hwl.lab.service.AreaService;
import com.hwl.lab.service.ChronicService;
import com.hwl.lab.unit.UnitData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin //允许跨域请求
@RestController
public class BaoxiaoController {
    @Autowired
    PeplesRepository peplesRepository;
    @Autowired
    ChronicService chronicService;
    @Autowired
    UnitData unitData;
    @Autowired
    ZhengRepository zhengRepository;
    @Autowired
    BaoxiaoRepository baoxiaoRepository;
    //查询农民是否参合
    @GetMapping("/findByChengid_B/{chengid}")
    public String findByChengid(@PathVariable("chengid") String chengid) {
        if (peplesRepository.findByChengid(chengid) != null) {
            if (!peplesRepository.findByChengid(chengid).getResig().equals("未参合")) {
                return "已参合";
            }else {
                return "该用户未参合";
            }
        } else {
            return "找不到数据";
        }
    }
    // 保存报销信息
    @PostMapping("/saveBaoxiao")
    public void save(@RequestBody(required=false)Baoxiao baoxiao) {
        baoxiao.setBaotime(unitData.getTime());
        Chronic chronic;
        chronic = chronicService.findByChengid(baoxiao.getChengid());
        chronic.setMoney(Integer.valueOf(baoxiao.getMoney())+Integer.valueOf( chronicService.findByChengid(baoxiao.getChengid()).getMoney()));
        chronicService.saveChronic(chronic);
        baoxiaoRepository.save(baoxiao);
    }
    // 保存报销信息
    @PostMapping("/saveBaoxiao1")
    public void save1(@RequestBody(required=false)Baoxiao baoxiao) {
        baoxiao.setStatus("通过");
        baoxiaoRepository.save(baoxiao);
    }
    // 保存报销信息
    @PostMapping("/saveBaoxiao2")
    public void save2(@RequestBody(required=false)Baoxiao baoxiao) {
        baoxiao.setStatus("不通过");
        baoxiao.setBaotime(unitData.getTime());
        Chronic chronic;
        chronic = chronicService.findByChengid(baoxiao.getChengid());
        chronic.setMoney(Integer.valueOf( chronicService.findByChengid(baoxiao.getChengid()).getMoney())-Integer.valueOf(baoxiao.getMoney()));
        chronicService.saveChronic(chronic);
        baoxiaoRepository.save(baoxiao);
    }
    // 获取农民信息
    @GetMapping("/findByChengid_B_F/{chengid}")
    public Peples peples(@PathVariable("chengid") String chengid) {
        return peplesRepository.findByChengid(chengid);
    }
    //判断信息是否合法
    @GetMapping("/testmsg/{chengid}/{chronicname}/{money}")
    public String testmsg(@PathVariable("chengid") String chengid, @PathVariable("chronicname") String chronicname,@PathVariable("money") String money ) {
        chronicService.findByChengid(chengid);
        if (chronicService.findByChengid(chengid) != null) {
            if (chronicService.findByChengid(chengid).getChronicname().equals(chronicname)) {
                if (testtime(chengid)) {
                    return money(chengid,money);
                } else {
                    return "当前时间不在报销时间范围内";
                }
            } else {
                return "该疾病与慢性病证中不一致";
            }
        } else {
            return "";
        }
    }
    // 判断日期是否合法
    public boolean testtime(String chengid) {
        //获取起始时间
        String startime = chronicService.findByChengid(chengid).getStartime();
        int statime_year = Integer.valueOf(startime.substring(0,4));
        int startime_month = Integer.valueOf(startime.substring(5,7));
        int startime_day = Integer.valueOf(startime.substring(8,10));
        System.out.println(statime_year+startime_day+startime_month);
        //获取截至时间
        String stoptime = chronicService.findByChengid(chengid).getStoptime();
        int stoptime_year = Integer.valueOf(stoptime.substring(0,4));
        int stoptime_month = Integer.valueOf(stoptime.substring(5,7));
        int stoptime_day = Integer.valueOf(stoptime.substring(8,10));
        //获取当前时间
        String nowtime = unitData.getTime();
        int nowtime_year = Integer.valueOf(nowtime.substring(0,4));
        int nowtime_month = Integer.valueOf(nowtime.substring(5,7));
        int nowtime_day = Integer.valueOf(nowtime.substring(8,10));
        if (nowtime_year>statime_year && nowtime_year<stoptime_year) {
            return true;
        } else if (nowtime_year == statime_year) {
            if (nowtime_month>startime_month) {
                return true;
            } else if (nowtime_month == startime_month) {
                if (nowtime_day>=startime_day) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else if (nowtime_year == stoptime_year) {
            if (nowtime_month<stoptime_month) {
                return true;
            } else if (nowtime_month == stoptime_month) {
                if (nowtime_day<=stoptime_day) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    // 计算报销金额
    public String money(String chengid,String money) {
        Chronic chronic = chronicService.findByChengid(chengid);
        String times = unitData.getTime().substring(0,4);
        Zheng zheng = zhengRepository.findByYears(times);
        int money1 = Integer.valueOf(money);
        int blibli = Integer.valueOf(zheng.getBlibli());
        int top = Integer.valueOf(zheng.getTop());
        int baoxiaojin = money1*blibli/100;
        if (baoxiaojin+chronic.getMoney()<=top) {
            return String.valueOf(baoxiaojin);
        } else {
            if ((top-chronic.getMoney())>0) {
                return String.valueOf(top-chronic.getMoney());
            } else {
                return "报销余额不足";
            }
        }
    }
    // 查询报销信息
    @GetMapping("/findBaoxiaoAll/{page}/{size}")
    public Page<Baoxiao> findAreaAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable=  PageRequest.of(page-1,size);
        return baoxiaoRepository.findAll(pageable);
    }
}
